package com.lam.system.service.impl;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.lam.common.utils.CollectionUtil;
import com.lam.common.utils.StringUtils;
import org.springframework.stereotype.Service;
import com.lam.system.mapper.SysPostMapper;
import com.lam.system.entity.SysPost;
import com.lam.system.param.SysPostParam;
import com.lam.system.service.SysPostService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lam.common.components.easyexcel.imp.model.AbstractImportBody;
import com.lam.common.components.easyexcel.imp.model.ImportEntry;
import com.lam.common.components.easyexcel.imp.model.ImportWrapper;
import com.lam.common.components.easyexcel.imp.service.AbstractImportService;
import com.lam.system.entity.imp.SysPostImp;
import com.lam.common.validate.model.UniqueDesc;
import com.lam.common.validate.ValidateUtil;
import com.lam.common.validate.model.ValidOperType;
import com.lam.common.constant.SystemConstant;
import java.text.MessageFormat;

/**
 * 职位信息Service业务层处理
 * 
 * @author admin
 * @date 2022-10-19
 */
@Service
public class SysPostServiceImpl extends AbstractImportService<SysPostImp, SysPost> implements SysPostService {

    @Autowired
    private SysPostMapper sysPostMapper;

    /**
     * 查询职位信息
     * 
     * @param id ID
     * @return 单个对象
     */
    @Override
    @Transactional(readOnly = true)
    public SysPost selectById(String id) {

        return sysPostMapper.selectById(id);
    }

    /**
     * 查询职位信息列表
     * 
     * @param param
     * @return 
     */
    @Override
    @Transactional(readOnly = true)
    public List<SysPost> selectList(SysPostParam param) {
    
        QueryWrapper<SysPost> queryWrapper = new QueryWrapper<>();
        
        //关键字查询
        if(StringUtils.isNotBlank(param.getKeywords())) {
            queryWrapper.and(wr->{
                wr.or().like("post_code", param.getKeywords());
                wr.or().like("post_name", param.getKeywords());
            });
        }
        
        queryWrapper.like(StringUtils.isNotBlank(param.getPostCodeKey()), "post_code", param.getPostCodeKey());
        queryWrapper.eq(StringUtils.isNotBlank(param.getPostCode()), "post_code", param.getPostCode());
        queryWrapper.like(StringUtils.isNotBlank(param.getPostNameKey()), "post_name", param.getPostNameKey());
        queryWrapper.eq(StringUtils.isNotBlank(param.getPostName()), "post_name", param.getPostName());
        queryWrapper.eq(Objects.nonNull(param.getStatus()), "status", param.getStatus());

        return sysPostMapper.selectList(queryWrapper);
    }

    /**
     * 新增职位信息
     * 
     * @param sysPost 
     * @return 
     */
    @Override
    @Transactional
    public int insert(SysPost sysPost) {
    
        return sysPostMapper.insert(sysPost);
    }

    /**
     * 修改职位信息
     * 
     * @param sysPost 
     * @return 
     */
    @Override
    @Transactional
    public int update(SysPost sysPost) {
    
        return sysPostMapper.updateById(sysPost);
    }

    /**
     * 删除职位信息信息
     * 
     * @param idStr ID，多个用逗号分隔
     * @return 
     */
    @Override
    @Transactional
    public int deleteById(String idStr) {
    	
        if(StringUtils.isBlank(idStr)) {
            return 0;
        }
        
        List<String> idList = StringUtils.str2List(idStr);
        QueryWrapper<SysPost> queryWrapper = new QueryWrapper<SysPost>().in("id", idList.size()>1?idList:idStr).select("post_code");
        List<String> list = sysPostMapper.selectList(queryWrapper).stream().map(t->t.getPostCode()).collect(Collectors.toList());
        if(CollectionUtil.isEmpty(list)) {
        	return 0;
        }
        ValidateUtil.validDataUsed("sys_user", "post", list, "职位有关联用户，不可删除");
        
        return sysPostMapper.deleteBatchIds(idList);
    }
    
    /**
     * 导入职位信息数据
     * @param wrapper 职位信息数据包装对象
     */
    @Override
    @Transactional
    public void importData(ImportWrapper<SysPostImp, SysPost> wrapper) {
        
        UniqueDesc uniqueDesc = ValidateUtil.selectUniqueDesc(SystemConstant.UNIQUE_POST_CODE);
        importData(wrapper, new AbstractImportBody<SysPostImp, SysPost>() {
            @Override
            public SysPost selectRecord(ImportEntry<SysPostImp, SysPost> entry) {
                return ValidateUtil.selectUniqueData(entry.getData(), ValidOperType.ADD, uniqueDesc, SysPost.class);
            }
            @Override
			public int insertData(SysPost data) {
				return insert(data);
			}
			@Override
			public int updateData(SysPost data) {
				return update(data);
			}
            @Override
            public String notUniqueMsg(ImportEntry<SysPostImp, SysPost> entry) {
                return MessageFormat.format("职位编码【{0}】已存在", entry.getData().getPostCode());
            }
        });
    }
    
}
